<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/9/21
 * Time: 14:48
 */
namespace app\modules\mch\controllers;
use yii\data\Pagination;
use app\models\ContentIndex;
use app\models\Store;
use app\models\Content;
use app\models\Goods;
use app\models\Cat;
use app\models\ContentGood;

use app\modules\mch\models\ContentIndexForm;
use app\modules\mch\models\ContentForm;
use app\modules\mch\models\GoodsForm;
use app\modules\mch\models\ContentGoodForm;

class ContentController extends Controller
{
    public function actionIndex()
    {

        $acid = $_COOKIE['70d4___uniacid'];
        $res = Store::find()->where(['acid' => $acid])->one();
        $wechat_app_id = $res['wechat_app_id'];
        $query = ContentIndex::find()->where(['wechat_app_id' => $wechat_app_id,'is_delete'=>0]);
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $datainfo = $query->limit($pagination->limit)->offset($pagination->offset)->asArray()->all();

        return $this->render('index', [
            'data' => $datainfo,
            'pagination'=>$pagination,
        ]);

    }

    public function actionSubjectEdit($id=null){
        $acid =$_COOKIE['70d4___uniacid'];
        $res=Store::find()->where(['acid'=>$acid])->one();
        $content = ContentIndex::findOne(['id'=>$id]);
        if(!$content){
            $content = new ContentIndex();
        }
        $form = new ContentIndexForm();
        $wechat_app_id = $res['wechat_app_id'];
        if(\Yii::$app->request->isPost) {

            $data = \Yii::$app->request->post();
//            var_dump($data);
            $form->wechat_app_id = $wechat_app_id;
            $form->title = $data['title'];
            $form->title_s = $data['title_s'];
            $form->is_delete = 0;
            $form->type = $data['type'];
            return json_encode($form->save($id), JSON_UNESCAPED_UNICODE);
        }

        $datainfo = ContentIndex::find()->where(['wechat_app_id'=>$wechat_app_id,'id'=>$id])->one();
        return $this->render('subject_edit', [
            'data'=>$datainfo
        ]);

    }
    public function actionSubjectDel($id=null){
        if(ContentIndex::updateAll(['is_delete'=>1],['id'=>$id])){
            $count = Content::find()->where(['is_delete'=>1,'subject_id'=>$id])->count();
            if($count>0) {
                if (Content::updateAll(['is_delete' => 1], ['subject_id' => $id])) {
                    return json_encode(array("code" => 0, "msg" => "删除成功"));
                }
            }else{
                return json_encode(array("code" => 0, "msg" => "删除成功"));
            }

        }
    }
    public function actionContentList(){
        $acid =$_COOKIE['70d4___uniacid'];
        $res=Store::find()->where(['acid'=>$acid])->one();
        $wechat_app_id = $res['wechat_app_id'];
        $query = Content::find()->alias('c')
            ->where(['c.wechat_app_id'=>$wechat_app_id,'c.is_delete'=>0])
            ->leftJoin('{{%content_subject}} s','s.id=c.subject_id');
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $list = $query
            ->select([
                'c.*','s.title subject_title'
            ])->limit($pagination->limit)->offset($pagination->offset)->asArray()->all();
        return $this->render('content-list',[
            'data'=>$list,
            'pagination'=>$pagination,
        ]);
    }
    public function actionContentEdit($id=null,$data=[]){
//        echo 1111;
        $acid = $_COOKIE['70d4___uniacid'];
        $res = Store::find()->where(['acid'=>$acid])->one();
        $wechat_app_id = $res["wechat_app_id"];
        $form = new ContentForm();
        $subject = ContentIndex::find()->where(['wechat_app_id'=>$wechat_app_id,'is_delete'=>0])->asArray()->all();

        if(\Yii::$app->request->isPost) {

            if(empty($data)) {
                $data = \Yii::$app->request->post();
            }
            $form->wechat_app_id = $wechat_app_id;
            $form->title = $data['title'];
            $form->title_s = $data['title_s'];
            $form->is_delete = 0;
            $form->content = $data['content'];
            $form->subject_id = $data['subject_id'];
            $form->image = $data['image'];
            $form->type = $data['type'];
            $form->video = $data['video'];
            $form->video_type = $data['video_type'];
            $res=$form->save($id);
            if($res->id!=''){
                $res->url = \Yii::$app->urlManager->createUrl(['mch/content/content-edit','id'=>$res->id]);
            }
//            var_dump($res);
//            if($res==null){
//                $res=[ 'code' => 0,
//                        'msg' => '保存成功',
//                    ];
//            }
            return json_encode($res, JSON_UNESCAPED_UNICODE);
        }
        $datainfo = Content::find()->where(['wechat_app_id' => $wechat_app_id, 'id' => $id])->one();
        return $this->render('content', [
            'data'=>$datainfo,
            'subject'=>$subject
        ]);
        }
    public function actionContentDel($id=null){
        if(Content::updateAll(['is_delete'=>1],['id'=>$id])){
            return json_encode(array("code"=>0,"msg"=>"删除成功"));


        }
    }
    public function actionGood($keyword = null){
        $data = \Yii::$app->request->post();
//
//       $datainfo='';
//
//           foreach ($data as $key => $value) {
//               if ($key == 'content' || $key == 'id'||$key=='goods_id') {
//                   continue;
//               } else {
//                   $datainfo = $datainfo . ';' . $value;
//               }
//           }

        $content=$data['content'];
        $id = $_GET['content_id'];
        $goods_id = $data['goods_id'];
//        var_dump($data);
        $query = Goods::find()->alias('g')->where(['g.store_id' => $this->store->id, 'g.is_delete' => 0]);
        $query->leftJoin(['c' => Cat::tableName()], 'c.id=g.cat_id');
        $cat_query = clone $query;
        $good_query = clone $query;
        $good_query->leftJoin(['cg' => ContentGood::tableName()], 'cg.goods_id=g.id')->where(['cg.content_id'=>$id,'cg.is_delete'=>0]);
        if (trim($keyword)) {
            $query->andWhere([
                'OR',
                ['LIKE', 'g.name', $keyword],
                ['LIKE', 'c.name', $keyword],
            ]);
        }
        if (isset($_GET['cat'])) {
            $cat = trim($_GET['cat']);
            $query->andWhere(['like', 'c.name', $cat]);
        }
        $per = $_GET['per-page'];
        $cat_list = $cat_query->groupBy('g.cat_id')->orderBy(['g.cat_id' => SORT_ASC])->select('c.name,g.cat_id')->asArray()->all();
        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->setPageSize(5);
        $pagination->defaultPageSize=5;
        $t = $_GET['page'];
        $page = $pages=1;
        if($per==''){
            $page = $t;
        }
//        echo $page;
        $pagination->setPage($page-1);
        $list = $query->orderBy('g.sort ASC,g.addtime DESC')->limit($pagination->limit)->offset($pagination->offset)->all();
        $good_count = $good_query->count();
        $good_pagination = new Pagination(['totalCount'=>$good_count]);
        $good_pagination->setPageSize(5);
//        $good_pagination->defaultPageSize=6;
        if($per!=''){
            $pages = $t;
        }
        $good_pagination->setPage($pages-1);
        $good_list = $good_query->limit($good_pagination->limit)->offset($good_pagination->offset)->all();
        return $this->render('good', [
            'list' => $list,
            'pagination' => $pagination,
            'cat_list' => $cat_list,
//            'data'=>$datainfo,
            'content'=>$content,
            'id'=>$id,
            'good_list'=>$good_list,
            'good_pagination' => $good_pagination,
        ]);
    }
    public function actionChoose(){
        $get = \Yii::$app->request->post();
        $res = 0;
        $goods_group = $get['goods_group'];
//        $content = $get['content'];
//        $datainfo2 = explode(';',$get['datainfo']);
//        $datainfo['title'] = $datainfo2[2];
//        $datainfo['title_s'] = $datainfo2[3];
//        $datainfo['image'] = $datainfo2[4];
//        $datainfo['subject_id'] = $datainfo2[5];
//        $datainfo['type'] = $datainfo2[6];
//        $datainfo['video_type'] = $datainfo2[7];
//        $datainfo['video'] = $datainfo2[8];
//        $datainfo['content'] = $content;
        $content_id=$get['id'];
//        $res = $this->actionContentEdit($id,$datainfo);
//       if($res=='null'){
//           $content_id =$id;
//       }else{
//           $content_id = $res->id;
//       }

        foreach($goods_group as $key =>$value){
//           var_dump($value);
            if($content_id!=''){
             $this->actionContentGood($value['id'],$content_id,0);
        }
        }
        return json_encode([
            'code'=>0,
            'msg'=>'成功',
            'id'=>$content_id,
        ]);
    }

    public function actionContentGood($goods_id=null,$content_id=null,$is_delete){
        $form = new ContentGoodForm();
        $form->content_id = $content_id;
        $form->store_id = $this->store->id;
        $form->goods_id = $goods_id;
        $model = ContentGood::findOne(['goods_id'=>$goods_id,'content_id'=>$content_id,'is_delete'=>0]);
        if(!$model){
            $model = new ContentGood();
        }
        $form->is_delete =$is_delete;
        $form->model = $model;
        $form->save();
    }
    public function actionConDel(){
        $get = \Yii::$app->request->post();
        $res = 0;
        $goods_group = $get['goods_group'];
        $content_id=$get['id'];
        foreach($goods_group as $key =>$value){
//           var_dump($value);
            if($content_id!=''){
                $this->actionContentGood($value['id'],$content_id,1);
            }
        }
        return json_encode([
            'code'=>0,
            'msg'=>'成功',
            'id'=>$content_id,
        ]);
    }

}